/*
 *      Copyright 2012-2013 Battams, Derek
 *       
 *       Licensed under the Apache License, Version 2.0 (the "License");
 *       you may not use this file except in compliance with the License.
 *       You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *       Unless required by applicable law or agreed to in writing, software
 *       distributed under the License is distributed on an "AS IS" BASIS,
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *       See the License for the specific language governing permissions and
 *       limitations under the License.
 */
package org.schedulesdirect.test.fvt;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;

import org.json.JSONObject;
import org.junit.Test;
import org.schedulesdirect.api.Headend;
import org.schedulesdirect.api.NetworkEpgClient;
import org.schedulesdirect.grabber.JsonRequest;
import org.schedulesdirect.grabber.JsonRequest.RequestAction;
import org.schedulesdirect.grabber.JsonRequest.RequestObject;
import org.schedulesdirect.grabber.Utils;
import org.schedulesdirect.test.TestUtils;

public class LineupManagement {
	
	@Test
	public void deleteHeadends() throws Exception {
		NetworkEpgClient clnt = null;
		try {
			clnt = TestUtils.getClientFor("valid");
			for(Headend he : clnt.getHeadends())
				clnt.removeHeadendFromAccount(he.getId());
			// Make sure we're always deleting at least one lineup
			clnt.addHeadendToAccount("PC:90210");
			clnt.removeHeadendFromAccount("PC:90210");
		} finally {
			if(clnt != null) clnt.close();
		}		
	}
	
	@Test
	public void addInvalidHeadend() throws Exception {
		NetworkEpgClient clnt = null;
		try {
			clnt = TestUtils.getClientFor("valid");
			clnt.addHeadendToAccount("_xBADx_");
		} catch (IOException e) {
			assertTrue(e.getMessage().contains("rc=401; msg=Invalid headend."));
		} finally {
			if(clnt != null) clnt.close();
		}
	}
	
	@Test
	public void addValidHeadend() throws Exception {
		NetworkEpgClient clnt = null;
		try {
			clnt = TestUtils.getClientFor("valid");
			clnt.addHeadendToAccount("PC:90210");
		} finally {
			if(clnt != null) clnt.close();
		}
	}
	
	@Test
	public void submitRequestWithoutHeadend() throws Exception {
		NetworkEpgClient clnt = null;
		try {
			clnt = TestUtils.getClientFor("valid");
			JsonRequest req = new JsonRequest(clnt.getHash(), clnt.getUserAgent(), clnt.getBaseUrl());
			JSONObject resp = req.submitForJson(RequestAction.ADD, RequestObject.HEADENDS, null);
			assertTrue(Utils.isErrorResponse(resp));
		} finally {
			if(clnt != null) clnt.close();
		}
	}
	
	@Test
	public void addValidHeadendOverLimit() throws Exception {
		String[] lineups = new String[] {"0005986", "0005993", "0005995", "0008999", "0009995", "0009999", "0005285"};
		NetworkEpgClient clnt = TestUtils.getClientFor("valid");
		for(String id : lineups) {
			try {
				clnt.addHeadendToAccount(id);
			} catch(IOException e) {
				if(e.getMessage().contains("rc=401; msg=Exceeded number of headends"))
					break;
				else
					throw e;
			}
		}
		for(Headend he : clnt.getHeadends())
			clnt.removeHeadendFromAccount(he.getId());
	}
	
	@Test
	public void addAlreadyRegisteredHeadend() throws Exception {
		NetworkEpgClient clnt = TestUtils.getClientFor("valid");
		clnt.addHeadendToAccount("0005986");
		try {
			clnt.addHeadendToAccount("0005986");
		} catch(IOException e) {
			if(e.getMessage().contains("Duplicate headend."))
				return;
			else
				throw e;
		}
		fail("Expected exception not thrown!");
	}
	
	@Test
	// This test will only pass once per 24 hours unless you get the SD team
	// to reset the "limited" account for you
	public void addTooManyHeadends() throws Exception {
		NetworkEpgClient clnt = TestUtils.getClientFor("limited");
		for(Headend he : clnt.getHeadends())
			clnt.removeHeadendFromAccount(he.getId());
		int remaining;
		int prevRemaining = -1;
		do {
			remaining = clnt.addHeadendToAccount("0005986");
			if(prevRemaining != -1 && remaining == prevRemaining)
				fail("changesRemaining is not decreasing!");
			prevRemaining = remaining;
			clnt.removeHeadendFromAccount("0005986");
			if(remaining == 0) {
				try {
					clnt.addHeadendToAccount("PC:90210");
					fail("Expected exception not thrown!");
				} catch(IOException e) {
					assertTrue(e.getMessage().contains("today"));
				}
			}
		} while(remaining > 0);
	}
	
	@Test
	public void searchForHeadends() throws Exception {
		NetworkEpgClient clnt = TestUtils.getClientFor("valid");
		assertTrue(clnt.getHeadends("K7P3G6", "CA").length >= 12);
	}
}
